package cn.edu.sgu.www.mhxysy.controller.role;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.role.RoleAccountDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleConfigSchemaDTO;
import cn.edu.sgu.www.mhxysy.dto.role.RoleConfigSchemaSaveDTO;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAttribute;
import cn.edu.sgu.www.mhxysy.entity.role.RoleConfigSchema;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.role.RoleConfigSchemaService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色配置方案管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_config_schema", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleConfigSchemaController {

    private final RoleConfigSchemaService roleConfigSchemaService;

    @Autowired
    public RoleConfigSchemaController(RoleConfigSchemaService roleConfigSchemaService) {
        this.roleConfigSchemaService = roleConfigSchemaService;
    }

    @ApiOperation("保存角色配置方案")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult<Void> save(@Validated RoleConfigSchemaSaveDTO roleConfigSchemaSaveDTO) {
        roleConfigSchemaService.save(roleConfigSchemaSaveDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加角色配置方案")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated RoleConfigSchemaDTO roleConfigSchemaDTO) {
        roleConfigSchemaService.insert(roleConfigSchemaDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID修改方案名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色配置方案ID", required = true),
            @ApiImplicitParam(name = "name", value = "方案名称", required = true)
    })
    @RequestMapping(value = "/updateNameById", method = RequestMethod.POST)
    public JsonResult<Void> updateNameById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = "方案名称" + CustomMessages.NOT_NULL) String name) {
        roleConfigSchemaService.updateNameById(id, name);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过角色ID查询角色开启的配置方案")
    @RequestMapping(value = "/selectOpenSchema", method = RequestMethod.GET)
    public JsonResult<RoleConfigSchema> selectOpenSchema(
            @ApiParam(name = "roleId", value = "角色ID", required = true)
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
        RoleConfigSchema roleConfigSchema = roleConfigSchemaService.selectOpenSchema(roleId);

        return JsonResult.success(null, roleConfigSchema);
    }

    @ApiOperation("通过角色ID查询角色配置方案列表")
    @RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
    public JsonResult<List<RoleConfigSchema>> selectByRoleId(
            @ApiParam(name = "roleId", value = "角色ID", required = true)
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
        List<RoleConfigSchema> list = roleConfigSchemaService.selectByRoleId(roleId);

        return JsonResult.success(null, list);
    }

}